//
//  MyOrderViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/3/28.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MyOrderViewController.h"
#import "CollectTreaTableViewCell.h"
#import "ShopDetailViewController.h"
@interface MyOrderViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * array;
@property (nonatomic, assign) int pageNo;

@end

@implementation MyOrderViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.pageNo=1;
    [self.array removeAllObjects];
    [self loadCollectList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //[self loadCollectList];
    self.array=[NSMutableArray array];
    [self.view addSubview:self.tableView];
    [self showTempView:@"暂无收藏!" Image:@"img_fail_monk" CGFrame:CGRectMake(0, 0, MainW,MainH-64)];
    self.tempView.hidden=YES;
    self.title=MyString(@"我的收藏");
    // Do any additional setup after loading the view.
}
-(UITableView*)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainW,MainH-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.backgroundColor=Gray_Color;
        _tableView.dataSource = self;
        _tableView.separatorStyle=0;
        [_tableView registerNib:[UINib nibWithNibName:@"CollectTreaTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"collectTreaCell"];
        _tableView.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self.array removeAllObjects];
            [self loadCollectList];
        }];
        [_tableView.mj_footer beginRefreshing];
    }
    return _tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.array.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 7.5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=self.array[indexPath.section];
    CollectTreaTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"collectTreaCell"];
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:dic[@"imageUrl"]] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    cell.nameLabel.text=dic[@"spuName"];;
    cell.detailLabel.text=[NSString stringWithFormat:@"工艺类型:%@",dic[@"saleName"]];
    cell.priceLabel.text=[NSString stringWithFormat:@"¥ %@",dic[@"price"]];
    cell.deleteBtn.tag=indexPath.section;
    [cell.deleteBtn addTarget:self action:@selector(deleteShopClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic=self.array[indexPath.section];
    ShopDetailViewController *vc=[[ShopDetailViewController alloc]init];
    vc.spuId=dic[@"skuId"];
    [self.navigationController pushViewController:vc animated:YES];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view=[UIView new];
    view.backgroundColor=Gray_Color;
    return view;
}
-(void)deleteShopClick:(UIButton *)sender{
    LXAlertView *alert=[[LXAlertView alloc] initWithTitle:nil message:MyString(@"确定删除") cancelBtnTitle:MyString(@"取消") otherBtnTitle:MyString(@"确定") clickIndexBlock:^(NSInteger clickIndex) {
        if (clickIndex==0) {
            NSLog(@"取消");
        }else{
            [self deleteCollectionCilck:sender];
            NSLog(@"确定");
        }
    }];
    //alert.animationStyle=LXASAnimationTopShake;
    [alert showLXAlertView];
    
}
-(void)loadCollectList{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"pageNumber"]=@(self.pageNo);
    dic[@"pageSize"]=@(10);
    [self doHttpRequestWithApi:API_COLLOCTLIST method:GET dic:dic successBlock:^(NSDictionary *dic) {
       [self.array addObjectsFromArray:dic[@"data"][@"list"]];
        if (self.array.count==0) {
            self.tempView.hidden=NO;
        }else{
            self.tempView.hidden=YES;
        }
        [self.tableView reloadData];
        [_tableView.mj_footer endRefreshing];
        
    }];
}
-(void)deleteCollectionCilck:(UIButton *)sender{
    NSDictionary *d=self.array[sender.tag];
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    dic[@"spuId"]=d[@"spuId"];
    dic[@"skuId"]=d[@"skuId"];
    [ self doHttpRequestWithApi:API_SHOPREMOVECLT method:GET dic:dic successBlock:^(NSDictionary *dic) {
        self.pageNo=1;
        [self.array removeAllObjects];
        [self loadCollectList];
    }];
}
@end
